﻿import flash.filters.BlurFilter;
dynamic class RotationBuilder extends MovieClip{
	
	private var links:Array;
	private var items:Array;
	private var nowAngle:Number = Math.PI/2;
	
	private var _maxX:Number = 300;
	private var _maxY:Number = 70;
	
	private var _maxScale:Number = 100;
	private var _minScale:Number = 60;
	private var _maxAlpha:Number = 100;
	private var _minAlpha:Number = 60;
	
	private var _maxBlueX:Number = 30;
	private var _maxBlueY:Number = 30;
	private var _minBlueX:Number = 0;
	private var _minBlueY:Number = 0;
	
	public var AutoRotation:Number = 0;
	public var Enable:Boolean = true;
	
	private var rotationToAngle:Number = 0;
	
	public function RotationBuilder() {
		super();
		items = new Array();
		
		this.onEnterFrame = this.runfun;
	}
	
	public function NextLeft() {
		var pAngle = Math.PI * 2 / (links.length);
		for (var i = 0; i < items.length; i++) {
			var n = (nowAngle +Math.PI*2 + items[i].startAngle) % (Math.PI * 2);
			if (Math.abs(n-Math.PI/2) < (pAngle/2)) {
				var index = (i + 1) % items.length;
				//trace(i+"|"+index);
				rotationToItem(items[index]);
				break;
			}
		}
	}
	public function NextRight() {
		var pAngle = Math.PI * 2 / (links.length);
		for (var i = 0; i < items.length; i++) {
			var n = (nowAngle + items[i].startAngle) % (Math.PI * 2);
			if (Math.abs(n-Math.PI/2) < (pAngle/2)) {
				var index = (i + items.length - 1) % items.length;
				//trace(i+"|"+index);
				rotationToItem(items[index]);
				break;
			}
		}
	}
	
	/**
	 * rotation all movieclips to target someone
	 * @param	linkId
	 */
	public function rotationTo(linkId:String) {
		for (var i = 0; i < links.length; i++) {
			if (linkId == links[i]) {
				var item:MovieClip = items[i];
				AutoRotation = 0;
				rotationToAngle = item.startAngle;
				//trace(item.startAngle);
			}
		}
	}
	private function rotationToItem(item:MovieClip) {
		AutoRotation = 0;
		rotationToAngle = item.startAngle;
	}
	/**
	 * build rotation MovieClips
	 * @param	ar
	 */
	public function build(ar:Array) {
		if (ar == undefined || ar.length <= 0) {
			return;
		}
		links = ar;
		if (items.length > 0) {
			for (var i = 0; i < items.length; i++) {
				MovieClip(items[i]).removeMovieClip();
			}
		}
		iems = new Array();
		links = randomArr(links);
		
		var pAngle = Math.PI * 2 / (links.length);
		
		for (var i = 0; i < links.length; i++) {
			var mc:MovieClip = this.attachMovie(links[i], "Ins_" + links[i], this.getNextHighestDepth());
			mc.startAngle = pAngle * i;
			mc.forceSmoothing = true;
			mc.cacheAsBitmap = true;
			mc.RB = this;
			mc.linkId = links[i];
			mc.onRelease = clickItem;
			items.push(mc);
		}
	}
	
	private function clickItem() {
		//this.RB.rotationTo(this.linkId);
		this._parent.rotationTo(this.linkId);
	}
	
	private function runfun() {
		if (!Enable) return;
		nowAngle += AutoRotation;
		
		if(AutoRotation==0){
			var target:Number = (Math.PI / 2 - rotationToAngle);
			
			if (target > nowAngle) {
				if (Math.abs(target - nowAngle) > Math.PI) {
					target -= Math.PI*2;
				}
			}else {
				if (Math.abs(target - nowAngle) > Math.PI) {
					target += Math.PI*2;
				}
			}
			
			
			nowAngle += (target-nowAngle) / 20;
			
		}
		if(items.length>0){
			this.setItemPositaion();
		}
		
		nowAngle %= (Math.PI * 2);
	}
	
	
	private function setItemPositaion() {
		for (var i = 0; i < items.length; i++) {
			var pA:Number = items[i].startAngle + nowAngle;
			items[i]._x = _maxX * Math.cos(pA);
			items[i]._y = _maxY * Math.sin(pA);
			var bl:Number = (items[i]._y + _maxY) / (2 * _maxY);
			var scale:Number = (_maxScale-_minScale) * bl + _minScale;
			
			items[i]._xscale = items[i]._yscale = scale;
			items[i]._alpha = (_maxAlpha - _minAlpha) * bl + _minAlpha;
			var bluex:Number = (_maxBlueX - _minBlueX) * (1-bl) + _minBlueX;
			var bluey:Number = (_maxBlueY - _minBlueY) * (1-bl) + _minBlueY;
			
			MovieClip(items[i]).filters = [new BlurFilter(bluex,bluey, 2)];
			MovieClip(items[i]).swapDepths(int((bl) * 10000));
			items[i].bl = bl;
		}
	}
	
	
	
	
	private function randomArr(ar:Array):Array {
		if (ar == undefined || ar.length <= 0) return;
		
		for (var i = 0; i < ar.length; i++) {
			var tempindex = int(Math.random() * ar.length);
			var tempitem = ar[tempindex];
			ar[tempindex] = ar[i];
			ar[i] = tempitem;
		}
		
		return ar;
	}
}
